#include "InstanceMap.h"
//-------------------------------------------------------------------------------------------------------
InstanceMap::InstanceMap(
	ID3D11Device*	device,				// Pointer to the application's device.		
	const string&	meshID,				// The ID of instances to store in InstanceMap.
	const string&	mapFilename)		// The map.txt file to read instance data from.
{ 
	bool result;

	// Read from map.txt file to generate instance data and build the dynamic vertex buffer.
	result = Initialise(device, meshID, mapFilename);

	// Output error message if the CPU instanced data or dynamic vertex buffer failed to initialise.
	if (!result)
	{
		MessageBox(NULL, L"Unable to create instance map!", L"Error", MB_OK);
	}
}

//-------------------------------------------------------------------------------------------------------
InstanceMap::~InstanceMap()
{
	Shutdown();
}

//-------------------------------------------------------------------------------------------------------
bool InstanceMap::Initialise(
	ID3D11Device* device,				// Pointer to the application's device.		
	const string& meshID, 				// The ID of instances to store in InstanceMap.
	const string& mapFilename)			// The map.txt file to read instance data from.
{
	// Create instanced data and dynamic instanced buffer.
	m_InstancedBuffer = ModelUtil::BuildSpecificInstancedBuffer(
		device,
		m_instancedData,
		meshID,
		mapFilename);

	// Initialise member variables.
	m_numVisible = 0;

	return true;
}

//-------------------------------------------------------------------------------------------------------
void InstanceMap::Shutdown()
{
	// Release the dynamic vertex buffer.
	ReleaseCOM(m_InstancedBuffer);
}

//-------------------------------------------------------------------------------------------------------
ID3D11Buffer* InstanceMap::GetInstancedBuffer() const
{
	// Return a pointer to the dynamic vertex buffer.
	return m_InstancedBuffer;
}

//-------------------------------------------------------------------------------------------------------
UINT InstanceMap::GetNumVisible() const
{
	// Return visible instance count.
	return m_numVisible;
}

//-------------------------------------------------------------------------------------------------------
void InstanceMap::SetNumVisible(UINT num)
{
	// Update the number of visible instances.
	m_numVisible = num;
}

//-------------------------------------------------------------------------------------------------------
vector<InstancedData>* InstanceMap::GetInstancedDataP()
{
	// Return a pointer to the vector containing every instance's data (world matrix and colour value).
	return (&m_instancedData);
}